package org.ljy.forum6.services.impl;

import lombok.extern.slf4j.Slf4j;
import org.ljy.forum6.common.AppResult;
import org.ljy.forum6.common.ResultCode;
import org.ljy.forum6.dao.MessageMapper;
import org.ljy.forum6.exception.ApplicationException;
import org.ljy.forum6.model.Message;
import org.ljy.forum6.model.User;
import org.ljy.forum6.services.IMessageService;
import org.ljy.forum6.services.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;
    /**
     * 发送站内信
     * @param message
     */
    @Override
    public void create(Message message) {
        //1.参数校验
        if(message == null || message.getPostUserId() == null || message.getContent() == null
        || message.getReceiveUserId() == null) {
            log.warn("参数有问题");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.检验接受者
        User user = userService.selectById(message.getReceiveUserId());
        if(user == null || user.getDeleteState() == 1) {
            log.warn("接受者不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.设置对象
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date now = new Date();
        message.setCreateTime(now);
        message.setUpdateTime(now);
        //3.调用dao
        int row = messageMapper.insert(message);
        if(row != 1) {
            log.warn("插入失败");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //1.参数验证
        User user = userService.selectById(receiveUserId);
        if(user == null || user.getDeleteState() == 1) {
            log.warn("用户不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用dao
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if(count < 0) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
        }
        return count;
    }


    /**
     * 返回站内信列表
     * @param receiveUserId 接受者id，也就是当前登陆的用户
     * @return
     */
    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //1.参数验证
        User user = userService.selectById(receiveUserId);
        if(user == null || user.getDeleteState() == 1) {
            log.warn("用户不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);

        return messages;
    }

    /**
     * 根据id查询站内信
     * @param id 站内信Id
     * @return Message
     */
    @Override
    public Message selectById(Long id) {
        //1.参数验证
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用dao
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    /**
     * 读取站内信
     * @param id 站内信id
     * @param state 0未读  1已读  默认0
     */
    @Override
    public void updateStateById(Long id, Byte state) {
        //1.参数检验
        if(id == null || id <= 0 || state >2 || state < 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.构造更新对象
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date now = new Date();
        updateMessage.setUpdateTime(now);
        //3.调用dao
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row != 1) {
            log.warn("更新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //1.检验参数
        if(repliedId == null || repliedId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.校验待回复站内信
        Message exitMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(exitMessage == null || exitMessage.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //3.更新已回复
        updateStateById(repliedId, (byte) 2);
        //4.发送回复私信
        create(message);
    }
}
